package com.gxa.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.commons.R;
import com.gxa.order.mapper.RoomMapper;
import com.gxa.order.pojo.hotel.Room;
import com.gxa.order.pojo.hotel.RoomType;
import com.gxa.order.service.RoomService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {
    @Resource
    private RoomMapper roomMapper;
    /**
     * 添加房间
     * @param room
     * @return
     */
    @Override
    public R addRoom(Room room) {
        R r = new R();
        room.setCreateTime(LocalDateTime.now());
        int insert = roomMapper.insert(room);
        //如果添加为上架房间，对应房型的房间总数量+1
        if(room.getStatus()==0){

        }
        try{
            if(insert>0){
                r.setCode(200);
                r.setMsg("success");
                //新增房间成功后，给酒店房间数+1

            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统繁忙");
        }
        return r;
    }

    /**
     * 查询房间
     * @param hotelId
     * @return
     */
    @Override
    public R queryRoom(Integer hotelId,String typeName) {
        R r = new R();
        HashMap<Object, Object> map = new HashMap<>();
        try{
            if(typeName==null||"".equals(typeName)){
                QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("hotel_id",hotelId);
                List<Room> room = roomMapper.selectList(queryWrapper);
                int size = room.size();
                map.put("全部总数",size);
                map.put("rooms",room);
                r.setCode(200);
                r.setData(map);
            }else {
                QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("type_name",typeName);
                List<Room> room = roomMapper.selectList(queryWrapper);
                int size = room.size();
                map.put("房间数量",size);
                map.put("rooms",room);
                r.setCode(200);
                r.setData(map);
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统繁忙");
        }
        return r;
    }

    /**
     * 修改房间
     * @param room
     * @return
     */
    @Override
    public R updateRoom(Room room) {
        R r = new R();
        try {
            int update = roomMapper.updateById(room);
            if(update>0){
                r.setCode(200);
                r.setMsg("success");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统繁忙");
        }
        return r;
    }

    /**
     * 上下架房间
     * @param roomId
     * @return
     */
    @Override
    public R downRoom(Integer roomId) {
        R r = new R();
        //查询房间上下架状态
        Room room = roomMapper.selectById(roomId);
        Integer status = room.getStatus();
        //获取到
        Integer hotelId = room.getHotelId();
        try{
            UpdateWrapper<Room> updateWrapper = new UpdateWrapper<>();
            if(status==0) {//房型是上架，改为下架状态
                updateWrapper.eq("room_id",roomId)
                        .set("status",1);
                //酒店房间数量-1,房间类型对应房间数-1

            boolean down = this.update(updateWrapper);
                if(down){
                    r.setCode(200);
                    r.setMsg("success");
                }
            }else {//房型是下架的，改为上架
                updateWrapper.eq("room_id",roomId)
                        .set("status",0);
                //酒店房间数量+1，房间类型对应房间数+1

                boolean down = this.update(updateWrapper);
                if(down){
                    r.setCode(200);
                    r.setMsg("success");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统繁忙");
        }
        return r;
    }

}

